package hpu.bitenergy.servlet;

import hpu.bitenergy.Factory.Factory;
import hpu.bitenergy.IFactory.IFactory;
import hpu.bitenergy.List.GetList;
import hpu.bitenergy.util.QueryHelper;
import hpu.bitenergy.util.StringHelper;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mongodb.DBObject;

public class expert_GLTservlet extends HttpServlet {

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		String expert_name = new String(request.getParameter("expert_name")
				.getBytes("ISO_8859-1"), "UTF-8");
		String label = new String(request.getParameter("label").getBytes(
				"ISO_8859-1"), "UTF-8");
		

		System.out.println("关系图：expert_name" + expert_name + "label: " + label);

		IFactory fa = Factory.GetIFactory("L");
		GetList li = fa.GetListInstance();
		List<DBObject> list = li.getQueryList(
				QueryHelper.expert_name(StringHelper.AddOneKong(expert_name)),
				QueryHelper.author_cn(true));
		// 拿到第一层数据
		DBObject db = StringHelper.GetAuther_nameListTest(list, expert_name);
		@SuppressWarnings("unchecked")
		List<String> nameList = (List<String>) db.get(expert_name);

		// 存取关联专家的专家
		List<DBObject> dbList = new ArrayList<DBObject>();
		//label小于0为第一层数据
		if (Integer.parseInt(label) > 0) {
			if (nameList.size() > 0) {
				// 有关联专家的人
				for (String name : nameList) {
					List<DBObject> list1 = li.getQueryList(QueryHelper
							.expert_name(StringHelper.AddOneKong(name)),
							QueryHelper.author_cn(true));
					// 拿到第一层数据
					DBObject db1 = StringHelper.GetAuther_nameListTest(list1,
							name);
					dbList.add(db1);
				}
			} else {
				// 没有关联专家的人
			}
		}else{
			dbList.add(db);
		}
		System.out.println(dbList);

		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/json");
		PrintWriter out = response.getWriter();

		out.write(dbList.toString());
		out.flush();
		out.close();
	}

}
